﻿using Coldairarrow.Business.View;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.View;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.View
{
    [Route("/View/[controller]/[action]")]
    public class View_WorkReportingDataController : BaseApiController
    {
        #region DI

        public View_WorkReportingDataController(IView_WorkReportingDataBusiness view_WorkReportingDataBus)
        {
            _view_WorkReportingDataBus = view_WorkReportingDataBus;
        }

        IView_WorkReportingDataBusiness _view_WorkReportingDataBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<View_WorkReportingData>> GetDataList(PageInput<WorkReportingSeatch> input)
        {
            return await _view_WorkReportingDataBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<View_WorkReportingData> GetTheData(IdInputDTO input)
        {
            return await _view_WorkReportingDataBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(View_WorkReportingData data)
        {
           
                await _view_WorkReportingDataBus.AddDataAsync(data);
           
        }
        /// <summary>
        /// 报工查询导出
        /// </summary>
        /// <param name="StartDate"></param>
        /// <param name="UserNames"></param>
        /// <returns></returns>
        [HttpGet]
        [NoCheckJWT]
        public async Task<IActionResult> GetExportView_WorkReporting(string StartDate,string UserNames)
        {
            PageInput<WorkReportingSeatch> input = new PageInput<WorkReportingSeatch>();
            if (!StartDate.IsNullOrEmpty()) {
                string[] number = StartDate.Split(',');
                List<string> List = new List<string>(number);
                input.Search.StartDate = List;
            }
            if (!UserNames.IsNullOrEmpty()) { 
              string[] UserName = UserNames.Split(',');
            List<string> List2 = new List<string>(UserName);
            input.Search.UserNames = List2;
            }
          
            var res = await _view_WorkReportingDataBus.GetDataListAsync(input);
            HSSFWorkbook book = new HSSFWorkbook();
            ICellStyle style6 = book.CreateCellStyle();

            style6.DataFormat = 194;
            ISheet s1 = book.CreateSheet("报工查询");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("日期");
            r1.CreateCell(1).SetCellValue("报工人");
            r1.CreateCell(2).SetCellValue("工序");
            r1.CreateCell(3).SetCellValue("编码");
            r1.CreateCell(4).SetCellValue("工件");
            r1.CreateCell(5).SetCellValue("派工数量");
            r1.CreateCell(6).SetCellValue("报工数量");
            r1.CreateCell(7).SetCellValue("完成数量");
            r1.CreateCell(8).SetCellValue("合格数量");
            for (int i = 0; i < res.Data.Count; i++)
            {
                IRow rt = s1.CreateRow(i + 1);
                rt.CreateCell(0).SetCellValue(res.Data[i].ReportTime.ToString());
                rt.CreateCell(1).SetCellValue(res.Data[i].ReportUserName);
                rt.CreateCell(2).SetCellValue(res.Data[i].ProcecureSubName);
                rt.CreateCell(3).SetCellValue(res.Data[i].MaterialCode);
                rt.CreateCell(4).SetCellValue(res.Data[i].MaterialName);
                rt.CreateCell(5).SetCellValue(Convert.ToDouble(res.Data[i].TaskQuantity== null ? "0" : res.Data[i].TaskQuantity.ToString()));
                rt.GetCell(5).CellStyle = style6;
                rt.CreateCell(6).SetCellValue(Convert.ToDouble(res.Data[i].Nums.ToString()));
                rt.GetCell(6).CellStyle = style6;
                rt.CreateCell(7).SetCellValue(Convert.ToDouble(res.Data[i].Completedquantity.ToString()));
                rt.GetCell(7).CellStyle = style6;
                rt.CreateCell(8).SetCellValue(Convert.ToDouble(res.Data[i].ReturnNums.ToString()));
                rt.GetCell(8).CellStyle = style6;

            }
            //输出的文件名称
            string fileName = "报工查询" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            book.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);



        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _view_WorkReportingDataBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}